﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace cludg.Infrastructure.DI
{
	public static class DIContainerHelper
	{
		public static void Register<TImpl>(this IDIContainer container, DILifetime lifetime, params object[] constructorParams)
		{
			container.Register(typeof(TImpl), lifetime, constructorParams);
		}

		public static void Register<TIntf, TImpl>(this IDIContainer container, string name, DILifetime lifetime, params object[] constructorParams) where TImpl : TIntf
		{
			container.Register(typeof(TIntf), typeof(TImpl), name, lifetime, constructorParams);
		}

		public static void Register<TIntf, TImpl>(this IDIContainer container, DILifetime lifetime, params object[] constructorParams) where TImpl : TIntf
		{
			Register<TIntf, TImpl>(container, null, lifetime, constructorParams);
		}

		public static void RegisterInstance<TIntf>(this IDIContainer container, TIntf instance, string name)
		{
			container.RegisterInstance(typeof(TIntf), instance, name);
		}

		public static void RegisterInstance<TIntf>(this IDIContainer container, TIntf instance)
		{
			RegisterInstance<TIntf>(container, instance, null);
		}

		public static TIntf Resolve<TIntf>(this IDIContainer container, string name)
		{
			return (TIntf)(container.Resolve(typeof(TIntf), name));
		}

		public static TIntf Resolve<TIntf>(this IDIContainer container)
		{
			return Resolve<TIntf>(container, null);
		}

		public static IEnumerable<TIntf> ResolveAll<TIntf>(this IDIContainer container)
		{
			return container.ResolveAll(typeof (TIntf)).Cast<TIntf>();
		}
	}
}
